package me.lxw.blogsystem.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import me.lxw.blogsystem.domain.Nav;
import me.lxw.blogsystem.utils.DBUtils;

/**
 * 文章数据访问对象
 * 
 * @author 梁兴威
 * 
 */
public class NavDAO {

    public List<Nav> findAll() throws SQLException {
        // 1.获取链接
        Connection conn = DBUtils.getConnection();

        // 2.拼接SQL
        String sql = "select * from nav";
        // 3.获取执行者，发送SQL
        PreparedStatement st = conn.prepareStatement(sql);

        ResultSet rs = st.executeQuery();

        // 4.处理结果
        List<Nav> navList = new ArrayList<Nav>();
        while (rs.next()) {
            Nav nav = new Nav();
            nav.setId(rs.getInt("id"));
            nav.setTitle(rs.getString("title"));
            nav.setInside(rs.getBoolean("inside"));
            nav.setUrl(rs.getString("url"));
            nav.setBlank(rs.getBoolean("blank"));
            nav.setSort(rs.getInt("sort"));
            nav.setStatus(rs.getBoolean("status"));
            // id,title,inside,blank,url,sort,status
            navList.add(nav);
        }

        // 5.关闭资源
        DBUtils.closeResource(conn, st, rs);

        return navList;
    }
}
